package oracle.apps.financials.collection.adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.po.Customer;
import oracle.apps.financials.collection.po.PercentMetricsItemValueHolder;
import oracle.apps.financials.collection.po.RendererValueHolder;
import oracle.apps.financials.collection.util.ChartHelper;
import oracle.apps.financials.collection.util.MockDataFactory;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.TimeChart;
import org.achartengine.chart.XYChart;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.tools.LineSliderListener;
import org.achartengine.tools.TimeSliderRangeListener;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PercentCurrentAdapter extends BaseChartAdapter {

	private boolean isShowPercent91DaysDelinquent = false;
	private boolean isShowpriorYear = false;

	public PercentCurrentAdapter(Context context, Customer customer, ViewGroup graphViewGroup) {
		super(context, customer, graphViewGroup);
		this.xMax = new Date().getTime();
		this.xMin = MockDataFactory.getTime(new Date(), Calendar.MONTH, -6).getTime();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0) {
			graphViewGroup = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.percent_current_graph, parent, false);
			this.initGraphView(graphViewGroup);
			this.processListener(customer, graphViewGroup);
			convertView = graphViewGroup;
		} else {
			convertView = LayoutInflater.from(context).inflate(R.layout.text, parent, false);
		}

		return convertView;
	}

	public void processListener(Customer customer, ViewGroup graphViewGroup) {
		if (customer != null) {

			mOnRangeChangeListner = new TimeSliderRangeListener() {
				@Override
				public void onRangeChanged(double start, double end) {
					setXAxisRange(lineChart, start, end);
				}
			};

			lineSliderListener = new LineSliderListener() {
				@Override
				public void onMove(double x) {
					refreshPieChart(x);
				}

			};
			timeSliderChart.setTimeSliderRangeListener(mOnRangeChangeListner);
			lineChart.setLineSliderListener(lineSliderListener);
		}
	}

	public void setXAxisRange(GraphicalView mView, double start, double end) {
		super.setXAxisRange(mView, start, end);
		mView.postDelayed(new Runnable() {
			@Override
			public void run() {
				refreshPieChart();
			}
		}, XYChartTimeRangeAnimator.DURATION_TIME + 1000);

	}

	public void priorYear() {
		isShowpriorYear = !isShowpriorYear;
		this.notifyDataSetChanged();
	}

	public void percent91DaysDelinquent() {
		isShowPercent91DaysDelinquent = !isShowPercent91DaysDelinquent;
		this.notifyDataSetChanged();
	}

	@Override
	public void notifyDataSetChanged() {
		if (this.customer != null) {
			PercentMetricsItemValueHolder percentCurrent = this.customer.getMetrics().getPercentCurrent();

			refreshLineChart(percentCurrent);

			refreshTimeSliderChart(percentCurrent);

			refreshPieChart(this.currentX);
		}
	}

	public void refreshLineChart(PercentMetricsItemValueHolder percentCurrent) {
		RendererValueHolder rendererValue = getLineRendererValue(percentCurrent, false);
		rendererValue.setxMax(xMax);
		rendererValue.setxMin(xMin);
		if (lineChart != null) {
			((XYChart) lineChart.getChart()).setCurrentX(currentX);
			this.lineChart.DataSetChanged(ChartHelper.getChartSettingsForLineChart(rendererValue), ChartHelper.buildDateDataset(rendererValue));
		}
	}

	public void refreshPieChart() {
		refreshPieChart(((XYChart) lineChart.getChart()).getCurrentX());
	}

	public void refreshPieChart(double currentX) {
		this.currentX = currentX;
		PercentMetricsItemValueHolder percentCurrent = customer.getMetrics().getPercentCurrent();
		percentCurrent.setCurrentX(currentX);
		RendererValueHolder rendererValue = getPieRendererValue(percentCurrent);
		if (pieChart != null) {
			this.pieChart.DataSetChanged(ChartHelper.getPieCategoryRenderer(rendererValue), ChartHelper.buildCategoryDataset(rendererValue));
		}
	}

	public void refreshTimeSliderChart(PercentMetricsItemValueHolder percentCurrent) {
		RendererValueHolder rendererValue = getLineRendererValue(percentCurrent, true);
		if (!percentCurrent.getxValueList().isEmpty()) {
			List<Date> percentCurrentX = (List<Date>) percentCurrent.getxValueList().get(0);
			rendererValue.setxMax(percentCurrentX.get(percentCurrentX.size() - 1).getTime());
			rendererValue.setxMin(percentCurrentX.get(0).getTime());
		}
		if (timeSliderChart != null) {
			timeSliderChart.DataSetChanged(ChartHelper.getChartSettingsForTimeSlider(rendererValue), ChartHelper.buildDateDataset(rendererValue));
		}
	}

	@Override
	public GraphicalView getLineChart() {
		PercentMetricsItemValueHolder percentCurrent = customer.getMetrics().getPercentCurrent();
		RendererValueHolder rendererValue = getLineRendererValue(percentCurrent, false);
		rendererValue.setxMax(xMax);
		rendererValue.setxMin(xMin);
		return ChartFactory.getTimeChartView(context, ChartHelper.buildDateDataset(rendererValue),
				ChartHelper.getChartSettingsForLineChart(rendererValue), MockDataFactory.DATE_FORMAT);
	}

	public RendererValueHolder getLineRendererValue(PercentMetricsItemValueHolder percentCurrent, boolean isTimeSlider) {

		RendererValueHolder rendererValue = new RendererValueHolder(context);
		rendererValue.setShowChartTitle(false);
		rendererValue.setChartTitle("Percent Current");
		rendererValue.setxTitle("Date");
		rendererValue.setyTitle("Percent Current (%)");

		rendererValue.getMargins()[0] = 40;
		rendererValue.setyMin(0);
		rendererValue.setyMax(60);
		rendererValue.getyMinList().add(0.00);
		rendererValue.getyMaxList().add(60.00);
		rendererValue.setyLabelMarginTop(30);
		rendererValue.setxLegendMarginLeft(0.1f);

		rendererValue.setAxesColor(Color.BLACK);
		rendererValue.setLabelsColor(Color.rgb(51, 51, 51));

		if (!percentCurrent.getxValueList().isEmpty()) {
			rendererValue.getTitles().add("Percent Current");
			rendererValue.getSliderTextList().add("As of ");
			rendererValue.getSliderTextList().add("Percent Current ");
			
			rendererValue.getColors().add(isTimeSlider ? Color.rgb(169, 169, 169) : Color.rgb(13, 99, 228));
			//rendererValue.getColors().add(isTimeSlider ? Color.rgb(169, 169, 169) :Color.rgb(206, 178, 0));

			rendererValue.getPointStyles().add(PointStyle.POINT);

			rendererValue.getxValueList().add(percentCurrent.getxValueList().get(0));
			rendererValue.getyValueList().add(percentCurrent.getyValueList().get(0));
			rendererValue.getTypes().add(TimeChart.TYPE);

			if (!isTimeSlider) {
				rendererValue.setApplyBackgroundGradientColor(true);
				rendererValue.setFromBackgroundGradientColor(Color.argb(200, 222, 222, 222));
				rendererValue.setToBackgroundGradientColor(Color.argb(200, 201, 198, 201));
				if (isShowpriorYear) {
					rendererValue.getTitles().add("Prior Year");
					rendererValue.getSliderTextList().add("As of ");
					rendererValue.getSliderTextList().add("Prior Year ");
					
					rendererValue.getColors().add(Color.rgb(206, 178, 0));					
					
					rendererValue.getPointStyles().add(PointStyle.POINT);

					rendererValue.getxValueList().add(percentCurrent.getxValueList().get(5));
					rendererValue.getyValueList().add(percentCurrent.getyValueList().get(5));
					rendererValue.getTypes().add(TimeChart.TYPE);
				}
				if (isShowPercent91DaysDelinquent) {
					rendererValue.getTitles().add("Percent 91+ Days Delinquent");
					rendererValue.getSliderTextList().add("As of ");
					rendererValue.getSliderTextList().add("Percent 91+ Days Delinquent ");
					//rendererValue.getColors().add(Color.rgb(225, 46, 35));
					rendererValue.getColors().add(Color.rgb(20, 135, 0));
					
					rendererValue.getPointStyles().add(PointStyle.POINT);
					rendererValue.getxValueList().add(percentCurrent.getxValueList().get(4));
					rendererValue.getyValueList().add(percentCurrent.getyValueList().get(4));
					rendererValue.getTypes().add(TimeChart.TYPE);
				}
			}

			// 6 month
			rendererValue.setTimeSliderXLeftScale(0.91 * 2 / (0.01 * MockDataFactory.size));
		}

		return rendererValue;
	}

	@Override
	public GraphicalView getTimeSlideChart() {

		PercentMetricsItemValueHolder percentCurrent = customer.getMetrics().getPercentCurrent();
		RendererValueHolder rendererValue = getLineRendererValue(percentCurrent, true);
		if (!percentCurrent.getxValueList().isEmpty()) {
			List<Date> percentCurrentX = (List<Date>) percentCurrent.getxValueList().get(0);
			rendererValue.setxMax(percentCurrentX.get(percentCurrentX.size() - 1).getTime());
			rendererValue.setxMin(percentCurrentX.get(0).getTime());
		}
		XYMultipleSeriesRenderer renderer = ChartHelper.getChartSettingsForTimeSlider(rendererValue);
		XYMultipleSeriesDataset seriesDataset = ChartHelper.buildDateDataset(rendererValue);

		return ChartFactory.getTimeChartView(context, seriesDataset, renderer, " MMM yyyy");
	}

	@Override
	public GraphicalView getPieChart() {
		PercentMetricsItemValueHolder percentCurrent = customer.getMetrics().getPercentCurrent();
		RendererValueHolder rendererValue = getPieRendererValue(percentCurrent);
		return ChartFactory.getPieChartView(context, ChartHelper.buildCategoryDataset(rendererValue),
				ChartHelper.getPieCategoryRenderer(rendererValue));
	}

	public RendererValueHolder getPieRendererValue(PercentMetricsItemValueHolder percentCurrent) {

		int index = percentCurrent.findXIndexByValue(percentCurrent.getCurrentX());

		RendererValueHolder rendererValue = new RendererValueHolder(context);

		if (!percentCurrent.getxValueList().isEmpty()) {

			List<Double> percentCurrentY = percentCurrent.getyValueList().get(0);
			List<Double> percent1To30DaysDelinquentY = percentCurrent.getyValueList().get(1);
			List<Double> percent31To60DaysDelinquentY = percentCurrent.getyValueList().get(2);
			List<Double> percent61To90DaysDelinquentY = percentCurrent.getyValueList().get(3);
			List<Double> percent90OverDaysDelinquentY = percentCurrent.getyValueList().get(4);
			List<Date> xValue = (List<Date>) percentCurrent.getxValueList().get(0);

			rendererValue.setChartTitle("Percent Delinquent As of " + MockDataFactory.dateFormat.format(xValue.get(index)) + "  ");

			rendererValue.getDetailValueList().add(percentCurrentY.get(index));
			rendererValue.getDetailValueList().add(percent1To30DaysDelinquentY.get(index));
			rendererValue.getDetailValueList().add(percent31To60DaysDelinquentY.get(index));
			rendererValue.getDetailValueList().add(percent61To90DaysDelinquentY.get(index));
			rendererValue.getDetailValueList().add(percent90OverDaysDelinquentY.get(index));

			rendererValue.getTitles().add("Percent current " + percentCurrentY.get(index) + "%    ");
			rendererValue.getTitles().add("Percent 1-30 Days Delinquent " + percent1To30DaysDelinquentY.get(index) + "%");
			rendererValue.getTitles().add("Percent 31-60 Days Delinquent " + percent31To60DaysDelinquentY.get(index) + "%");
			rendererValue.getTitles().add("Percent 61-90 Days Delinquent " + percent61To90DaysDelinquentY.get(index) + "%");
			rendererValue.getTitles().add("Percent 91+ Days Delinquent " + percent90OverDaysDelinquentY.get(index) + "%");
		}

		return rendererValue;
	}

	public boolean isShowPercent91DaysDelinquent() {
		return isShowPercent91DaysDelinquent;
	}

	public void setShowPercent91DaysDelinquent(boolean isShowPercent91DaysDelinquent) {
		this.isShowPercent91DaysDelinquent = isShowPercent91DaysDelinquent;
	}

	public boolean isShowpriorYear() {
		return isShowpriorYear;
	}

	public void setShowpriorYear(boolean isShowpriorYear) {
		this.isShowpriorYear = isShowpriorYear;
	}

}